import dayjs from 'dayjs' // 文档 https://day.js.org/docs/en/display/format
import { ElLoading,ElMessage } from 'element-plus'
import './storage'

const utils = {
    dayjs: () => {
        // dayjs('2019-01-25').format('DD/MM/YYYY')
        return dayjs()
    },
    getNewObj: (obj) => {
        return JSON.parse(JSON.stringify(obj))
    },
    deepClone: (obj1, obj2 = {}) => {
        let toStr = Object.prototype.toString;
        let arrStr = toStr.call([]);
        for (let prop in obj1) {
            if (obj1.hasOwnProperty(prop)) {
                if (obj1[prop] !== null && typeof (obj1[prop]) == "object") {
                    obj2[prop] = toStr.call(obj1[prop]) == arrStr ? [] : {};
                    utils.deepClone(obj1[prop], obj2[prop]);
                } else {
                    obj2[prop] = obj1[prop];
                }
            }
        }
        return obj2;
    },
    setlocalStorage: (key, value) => {
        window.localStorage.setItem(key, window.JSON.stringify(value))
    },
    getlocalStorage: (key) => {
        return window.JSON.parse(window.localStorage.getItem(key))
    },
    removelocalStorage: (key) => {
        window.localStorage.removeItem(key)
    },
    setsessionStorage: (key, value) => {
        window.sessionStorage.setItem(key, window.JSON.stringify(value))
    },
    getsessionStorage: (key) => {
        return window.JSON.parse(window.sessionStorage.getItem(key))
    },
    removesessionStorage: (key) => {
        window.sessionStorage.removeItem(key)
    },
    setCookie: (name, value, Hours) => {
        if (Hours == null || Hours == '') {
            Hours = 0.5;
        }
        var exp = new Date();
        exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(window.JSON.stringify(value)) + "; path=/;expires=" + exp.toGMTString();
    },
    getCookie: (name) => {
        var arr,
            reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return window.JSON.parse(unescape(arr[2]));
        else
            return null;
    },
    removeCookie: (name) => {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = utils.getCookie(name);
        if (cval != null)
            document.cookie = name + "=" + cval + "; path=/;expires=" + exp.toGMTString();
    },
    eventEmitter: {
        list: {},
        $on: function (event, fn) {
            let _this = this;
            // 如果对象中没有对应的 event 值，也就是说明没有订阅过，就给 event 创建个缓存列表
            // 如有对象中有相应的 event 值，把 fn 添加到对应 event 的缓存列表里
            (_this.list[event] || (_this.list[event] = [])).push(fn);
            return _this;
        },
        $emit: function () {
            let _this = this;
            // 第一个参数是对应的 event 值，直接用数组的 shift 方法取出
            let event = [].shift.call(arguments),
                fns = [..._this.list[event]];
            // 如果缓存列表里没有 fn 就返回 false
            if (!fns || fns.length === 0) {
                return false;
            }
            // 遍历 event 值对应的缓存列表，依次执行 fn
            fns.forEach(fn => {
                fn.apply(_this, arguments);
            });
            return _this;
        }
    },
    trim: (str = "", type = "both") => {
        return (str + "").replace({
            both: /^\s+|\s+$/g,
            left: /^\s*/,
            right: /(\s*$)/g,
            all: /\s+/g
        }[type], "")
    },
}
export default utils
